//
//  AOMainController.swift
//  weibo_zha
//
//  Created by _zha on 16/10/24.
//  Copyright © 2016年 渣. All rights reserved.
//

import UIKit
import SVProgressHUD

class AOMainController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //实例化tabBar
        let tabBar = AOTabBar()
        //tabBar的闭包实例化
        tabBar.closure = {  [unowned self] in
            if !AOUserAccountViewModel.sharedTools.isLogin {
                
                SVProgressHUD.showError(withStatus: "请先登录, 亲~")
                return
            }
            //  代码执行到此,表示登录
            //  获取 window , 获取最上面的 window
//            let window = UIApplication.shared.windows.last!
            let composeView = AOComposeView()
            composeView.show(target: self)
        }
        //通过KVC给计算型属性赋值
        setValue(tabBar, forKey: "tabBar")
        //添加子控制器
        addChildViewController(vc: AOHomeController(), title: "首页", imgName: "tabbar_home")
        addChildViewController(vc: AOMessageController(), title: "消息", imgName: "tabbar_message_center")
        addChildViewController(vc: AODiscoverController(), title: "发现", imgName: "tabbar_discover")
        addChildViewController(vc: AOProfileController(), title: "我", imgName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imgName: String) {
        //设置标题
        vc.title = title
        //设置标题颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: UIControlState.selected)
        //设置图片
        vc.tabBarItem.image = UIImage(named:imgName)
        //设置选中时的图片
        vc.tabBarItem.selectedImage = UIImage(named:"\(imgName)_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //添加到nav上
        let nav = UINavigationController(rootViewController:vc)
        //添加到tabBar上
        addChildViewController(nav)
    }
    
}
